<?php

namespace app\common\model\lottery;

use think\Cache;
use app\common\model\BaseModel;

/**
 * 抽奖设置
 * Class Setting
 * @package app\common\model\lottery
 */
class Setting extends BaseModel
{
    protected $name = 'lottery_setting';
    protected $createTime = false;

    /**
     * 获取器: 转义数组格式
     */
    public function getValuesAttr($value)
    {
        return json_decode($value, true);
    }

    /**
     * 修改器: 转义成json格式
     */
    public function setValuesAttr($value)
    {
        return json_encode($value);
    }

    /**
     * 获取指定项设置
     */
    public static function getItem($key, $wxapp_id = null)
    {
        $data = static::getAll($wxapp_id);
        return isset($data[$key]) ? $data[$key]['values'] : [];
    }


    /**
     * 获取全部设置
     */
    public static function getAll($wxapp_id = null)
    {
        $model = new static;
        is_null($wxapp_id) && $wxapp_id = $model::$wxapp_id;
        if (!$data = Cache::get('lottery_setting_' . $wxapp_id)) {
            $data = array_column(collection($model::all())->toArray(), null, 'key');
            Cache::tag('cache')->set('lottery_setting_' . $wxapp_id, $data);
        }
        return array_merge_multiple($model->defaultData(), $data);
    }

     /**
     * 获取设置项信息
     */
    public static function detail($key)
    {
        return static::get(compact('key'));
    }


     /**
     * 默认配置
     * @return array
     */
    public function defaultData()
    {
        return [
            'basic' => [
                'key' => 'basic',
                'describe' => '基础设置',
                'values' => [
                    // 抽奖失败自动退款
                    'auto_refund' => '1',
                    // 是否允许使用优惠券
                    'is_coupon' => '1',
                    // 是否开启分销
                    'is_dealer' => '0',
                    // 抽奖规则 简述
                    'rule_brief' => '抽奖规则',
                    // 抽奖规则 详述
                    'rule_detail' => "规则",
                    // 拼单状态模板消息id
                    'tpl_msg_id' => '',
                    // 流程
                    'lottery_flow' =>'抽奖流程',
                    // 流程详情
                    'flow_detail'  =>'流程',
                ]
            ]
        ];
    }

}